﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WeifenLuo.WinFormsUI.Base
{
    public partial class FormBaseData : FormBase
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Winapi)]
        internal static extern IntPtr GetFocus();

        public Label lblStatus;
        public Button btnCancel;
        public Button btnOk;
        protected CustomDataGridView.CustomDataGrdView mdbg_currentGrid;
        public enum MODES : short { VOID = 0, FULL, MODIFY, ADD };
        private MODES me_currentMode, me_previousMode;
        protected string ms_currentField, ms_valueCurrentField;

        public string ms_ValueReturnList;
        protected CurrencyManager mRowManager;
        protected DataSet mds_Report;

        protected DataSet mObjListSet;
        //protected ReportClass mObjReport;
        protected int mi_CurrentRow, mi_currentCol = 0;
        protected bool mb_saved = false, mb_posting_reported = false;
        protected bool mb_IsListOpened = false, mb_AcceptDelete = false;

        private bool m_bPrintable = true;
        public bool Printable
        {
            get { return (m_bPrintable); }
            set { m_bPrintable = value; }
        }

        public FormBaseData()
        {
            InitializeComponent();
        }

        protected override void BasicForm_Load(object sender, EventArgs e)
        {
            base.BasicForm_Load(sender, e);
            try
            {
                if (this.Modal)
                {	// Don't check Access Right for Modal Dialog
                    LoadMasterData();
                }
                else
                {	// This form is MDI child form --> check permission.
                    if (mFormRight.mb_read)
                    {
                        Cursor = Cursors.WaitCursor;
                        LoadMasterData();
                        Cursor = Cursors.Default;
                    }
                    else
                    {	// Only show the empty form, no data
                        MessageBox.Show("Access denied. Please contact Administrator HRM System!", ConstantKeys.Title, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        SetDataManipulation(false);
                        this.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                Ctrl_LOG.Print("Base Data Form", "Basic Form Load", "Error message: '" + ex.Message + "'");
            }
        }

        protected override void BasicForm_Closing(object sender, FormClosingEventArgs e)
        {
            //base.BasicForm_Closing(sender, e);
            try
            {
                if (GetCurrentAction() != MODES.FULL && GetCurrentAction() != MODES.VOID)
                {
                    switch (MessageBox.Show("Are you sure ?", ConstantKeys.Title, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1))
                    {
                        case DialogResult.Yes:
                            cancel();
                            break;
                        case DialogResult.No:
                            e.Cancel = true;
                            break;
                    }
                }
                base.BasicForm_Closing(sender, e);
            }
            catch (Exception ex)
            {
                Ctrl_LOG.Print("In event Closing", "Error message: '" + ex.Message + "'");
            }
        }

        protected override void BaseForm_KeyDown(object sender, KeyEventArgs e)
        {
            base.BaseForm_KeyDown(sender, e);
            // Cannot make a navigation in the mode ADD and MODIFY
            if (GetCurrentAction() == MODES.ADD || GetCurrentAction() == MODES.MODIFY) return;
            
            switch (e.KeyCode)
            {
                case Keys.PageUp:
                    move_previous();
                    break;
                case Keys.PageDown:
                    move_next();
                    break;
                case Keys.Home:
                    move_first();
                    break;
                case Keys.End:
                    move_last();
                    break;
            }
        }

        public virtual void OpenSelection(string as_SelectioField)
        {	// This method is override in the master form that containts a grid 
            // It's also used in the master detail form

            // Set selection parameter structure
            // Call the Selection form
            // Switch to field that needs to select a value.
        }

        public virtual void RefreshData()
        {
            Cursor = Cursors.WaitCursor;
            ClearDataBinding(this);
            LoadMasterData();
            if (mRowManager != null)
            {
                lblStatus.Text = (mRowManager.Position + 1).ToString() + "/" + mRowManager.Count.ToString();
            }
            Cursor = Cursors.Default;
        }

        protected virtual void LoadMasterData()
        {	// This method is call after overriding (at the end of the overriding method)
            if (mRowManager == null)
            {
                void_mode();
                return;
            }
            mRowManager.PositionChanged += new EventHandler(RowMaster_Changed);
            mRowManager.ItemChanged += new ItemChangedEventHandler(ItemMaster_Changed);

            if (mRowManager.Count > 0)
                full_mode();
            else
                void_mode();
        }

        private Control LookUpControls(Control aCtrl)
        {
            foreach (Control control in aCtrl.Controls)
                if (aCtrl.Focused) return control;
            return this;
        }

        public void ClearDataBinding(Control aCtrl)
        {
            foreach (Control lCtrl in aCtrl.Controls)
            {
                switch (lCtrl.GetType().ToString())
                {
                    case "System.Windows.Forms.GroupBox":
                        GroupBox lctr_groupBox = (GroupBox)lCtrl;
                        ClearDataBinding(lctr_groupBox);
                        break;

                    case "System.Windows.Forms.TabControl":
                        TabControl lctr_tabControl = (TabControl)lCtrl;
                        ClearDataBinding(lctr_tabControl);
                        break;

                    case "System.Windows.Forms.TabPage":
                        TabPage lctr_tabPage = (TabPage)lCtrl;
                        ClearDataBinding(lctr_tabPage);
                        break;

                    case "System.Windows.Forms.Panel":
                        Panel lctr_panel = (Panel)lCtrl;
                        ClearDataBinding(lctr_panel);
                        break;

                    default:
                        lCtrl.DataBindings.Clear();
                        break;
                }
            }
        }

        public void CreateTextBoxButton(TextBox atxt)
        {
            Button btn = new Button();
            btn.Size = new Size(20, 20);
            btn.Location = new Point(atxt.Width - 23, -1);
            btn.Name = atxt.Name.Replace("txt", "btn");
            btn.Parent = atxt;
            btn.BringToFront();
            atxt.Controls.Add(btn);
            btn.BackColor = Color.Transparent;
            btn.Text = "";
            btn.Font = new Font("Wingdings 3", 8.25F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(2)));
            btn.Anchor = ((AnchorStyles)((AnchorStyles.Top | AnchorStyles.Right)));
            btn.Click += new EventHandler(btn_Click);
        }

        public virtual void btn_Click(object sender, EventArgs e)
        { }

        public virtual void RowMaster_Changed(object sender, EventArgs e)
        {
            if (GetCurrentAction() == MODES.FULL)
            {
                lblStatus.Text = (mRowManager.Position + 1).ToString() + "/" + mRowManager.Count.ToString();
                //if (mdbg_currentGrid != null) mdbg_currentGrid.Rows[mRowManager.Position].Selected = true;
            }
        }

        public virtual void ItemMaster_Changed(object sender, ItemChangedEventArgs e)
        {
        }

        public virtual void move_first()
        {
            if (mRowManager.Position == 0) return;
            mRowManager.Position = 0;
        }

        public virtual void move_previous()
        {
            if (mRowManager.Position == 0) return;
            mRowManager.Position--;
        }

        public virtual void move_next()
        {
            if (mRowManager.Position == mRowManager.Count - 1) return;
            mRowManager.Position++;
        }

        public virtual void move_last()
        {
            if (mRowManager.Position == mRowManager.Count - 1) return;
            mRowManager.Position = mRowManager.Count - 1;
        }

        public virtual void modify()
        {	// It should be called object base before overriding
            mb_saved = false;
            mi_CurrentRow = mRowManager.Position;
            SetPreviousAction(GetCurrentAction());
            SetCurrentAction(MODES.MODIFY);
            RefreshToolbar();
            lblStatus.Text = "[Modify]";
        }

        public virtual void add()
        {	// It should be called object base before overriding
            if (mRowManager == null) return;
            mb_saved = false;
            SetPreviousAction(GetCurrentAction());
            // Capture the currentrow 
            mi_CurrentRow = mRowManager.Position;
            SetCurrentAction(MODES.ADD);
            RefreshToolbar();
            lblStatus.Text = "[Add New]";
        }

        public virtual void delete()
        {	// It should be called object base before overriding, then check the flag mb_AcceptDelete to confirm for deleting
            string[] las_params = new string[1];
            las_params[0] = this.Text;
            lblStatus.Text = "[Delete]";

            if (MessageBox.Show("Are you delete '" + las_params[0] + "' ?", ConstantKeys.Title, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
                mb_AcceptDelete = true;
            else
            {
                mb_AcceptDelete = false;
                lblStatus.Text = (mRowManager.Position + 1).ToString() + "/" + mRowManager.Count.ToString();
            }
        }

        // This method must be called after the delete action finishing
        public virtual void post_delete()
        {// It should be called at the end of the overrided delete method
            if (mRowManager.Count > 0)
                full_mode();
            else
                void_mode();
        }

        public virtual void cancel()
        {	// It should be called object base after overriding
            mb_saved = true;
            // Captures the row that before adding 
            switch (GetCurrentAction())
            {
                case MODES.ADD:
                    mRowManager.Position = mi_CurrentRow;
                    break;
            }
            if (mRowManager.Count > 0)
                full_mode();
            else
                void_mode();
        }

        public virtual void save()
        {	// It should be called object base after overriding
            mb_saved = true;
            if (mRowManager.Count > 0)
            {
                switch (GetCurrentAction())
                {
                    case MODES.ADD:
                        mi_CurrentRow = mRowManager.Count - 1;
                        break;
                    case MODES.MODIFY:
                        mRowManager.Position = mi_CurrentRow;
                        break;
                }
                full_mode();
            }
            else
                void_mode();
        }

        public virtual void full_mode()
        {
            SetCurrentAction(MODES.FULL);
            RefreshToolbar();
            lblStatus.Text = (mRowManager.Position + 1).ToString() + "/" + mRowManager.Count.ToString();
        }

        public virtual void void_mode()
        {
            mi_CurrentRow = -1;
            SetCurrentAction(MODES.VOID);
            RefreshToolbar();
            lblStatus.Text = "0/0";
        }

        public void SetData2List(DataSet aObjSet)
        {
            mObjListSet = aObjSet;
        }

        public void list()
        {
            try
            {	// It should be called object base before overriding						
                FormList lfrm_List = new FormList(mObjListSet);
                lfrm_List.mfrmParentForm = this;
                lfrm_List.mMainForm = mMainForm;
                if (lfrm_List.ShowDialog() == DialogResult.OK)
                {
                    mRowManager.Position = lfrm_List.GetCurrentRow();
                    SetListChanged();
                }
                lfrm_List.Dispose();

                // Set status under mouse after view list
                if ((mMainForm != null) && (mMainForm.MdiChildren.Length >= 1))
                    mMainForm.stt_Bar.Text = "Ready ... ";
            }
            catch (Exception ex)
            {
                Ctrl_LOG.Print("BaseDataForm", "In method list", "Error message: '" + ex.Message + "'");
            }
        }

        public virtual void SetListChanged()
        {
            // Set Description, checkbox, radio button, ...
        }

        public virtual void SetCurrentAction(MODES ae_mode)
        {	// There are 4 modes: VOID, FULL, ADD, MODIFY
            me_currentMode = ae_mode;
        }

        public virtual MODES GetCurrentAction()
        {
            return me_currentMode;
        }

        public virtual void SetPreviousAction(MODES ae_mode)
        {
            me_previousMode = me_currentMode;
            me_currentMode = ae_mode;
        }

        public virtual MODES GetPreviousAction()
        {
            return me_previousMode;
        }

        protected virtual void reportposting()
        {
            // It should be called object base after overriding
            mb_posting_reported = true;
            print_preview();
        }

        protected virtual void posting()
        {	// It should be called object base after overriding
            mRowManager.PositionChanged += new EventHandler(RowMaster_Changed);

            if (mRowManager.Count > 0)
                full_mode();
            else
            {
                void_mode();
            }
        }

        protected virtual void postprint()
        {
            // It should be called object base after overriding
            posting();
            mb_posting_reported = true;
        }

        // Set Object report document Thang comment: 2012
        //public void SetObjReport(ReportClass aObjectRpt)
        //{
        //    mObjReport = aObjectRpt;
        //}

        protected virtual void FillDsReport_Posting()
        { }

        protected virtual void FillDsReport()
        { }

        public virtual void print()
        {
            // Show the Print dialog to print a report	
            if (mb_posting_reported)
            {
                FillDsReport_Posting();
            }
            else
            {
                FillDsReport();
            }
            mb_posting_reported = false;// Reset the posting reported flag

            //if (mObjReport == null) // Thang comment: 2012
            //{
            //    MessageBox.Show("Báo biểu không hợp lệ.", ConstantKeys.Title, MessageBoxButtons.OK, MessageBoxIcon.Stop);
            //    return;
            //}
            //mObjReport.PrintToPrinter(1, false, 0, 0);
        }

        protected override void RefreshToolbar()
        {	// It should be called the object base before overriding
            if (mRowManager == null)
            {
                SetDataManipulation(false);
                btnOk.Enabled = btnCancel.Enabled = false;
                return;
            }

            base.RefreshToolbar();
            bool lb_read = false, lb_add = false, lb_delete = false,
                 lb_modify = false, lb_print = false, lb_validate = false;

            switch (GetCurrentAction())
            {
                case MODES.VOID:
                    lb_read = lb_delete = lb_modify = lb_validate = lb_print = false;
                    lb_add = true;
                    break;

                case MODES.FULL:
                    lb_read = lb_add = lb_delete = lb_modify = lb_print = true;
                    lb_validate = false;
                    break;

                case MODES.ADD:
                    lb_read = lb_add = lb_delete = lb_modify = lb_print = false;
                    lb_validate = true;
                    break;

                case MODES.MODIFY:
                    lb_read = lb_add = lb_delete = lb_modify = lb_print = false;
                    lb_validate = true;
                    break;
            }

            // Set Security here
            lb_read &= mFormRight.mb_read;
            lb_add &= mFormRight.mb_add;
            lb_delete &= mFormRight.mb_delete;
            lb_modify &= mFormRight.mb_modify;
            lb_print &= mFormRight.mb_print;
            /////////////////////
            btnOk.Enabled = lb_validate;
            btnCancel.Enabled = lb_validate;
            /////////////////////
            SetNavigator(lb_read);
            SetRefreshData(lb_read);
            SetAdd(lb_add);
            SetModify(lb_modify);
            SetDelete(lb_delete);
            SetValidate(lb_validate);
            SetList(lb_read);
            SetPrint(lb_print);
            SetPrintPreview(lb_print);
            SetFind(false);
        }

        public DataGridView GetCurrentDataGrid()
        {
            return mdbg_currentGrid;
        }

        public void SetCurrentDataGrid(CustomDataGridView.CustomDataGrdView adbg_Grid)
        {
            mdbg_currentGrid = adbg_Grid;
        }

        // Thang comment
        //public void CreateButtonColumn(DataGrid adbg_Grid, string as_ColumnName, string bitmapName)
        //{
        //    adbg_Grid.Splits[0].DisplayColumns[as_ColumnName].Button = true;
        //    adbg_Grid.Columns[as_ColumnName].ButtonPicture = Image.FromFile(ms_Path + @"\ICons\" + bitmapName);
        //}

        //public void SetCheckbox(DataGrid adbg_Grid, string[] asa_ColumnNames)
        //{
        //    foreach (string ls_ColumnName in asa_ColumnNames)
        //    {
        //        adbg_Grid.Columns[ls_ColumnName].ValueItems.Presentation = C1.Win.C1TrueDBGrid.PresentationEnum.CheckBox;
        //        adbg_Grid.Columns[ls_ColumnName].ValueItems.CycleOnClick = true;
        //    }
        //}

        public void SetCheckbox(CustomDataGridView.CustomDataGrdView adbg_Grid, string[] asa_ColumnNames)
        {
            //foreach (string ls_ColumnName in asa_ColumnNames)
            //{
            //    adbg_Grid.Columns[ls_ColumnName].DefaultCellStyle.
            //    adbg_Grid.Columns[ls_ColumnName].ValueItems.Presentation = C1.Win.C1TrueDBGrid.PresentationEnum.CheckBox;
            //    adbg_Grid.Columns[ls_ColumnName].ValueItems.CycleOnClick = true;
            //}
        }

        public void mdbg_dataGrid_Enter(object sender, System.EventArgs e)
        {
            if (sender.GetType().ToString() == "CustomDataGridView.CustomDataGrdView")
                SetCurrentDataGrid((CustomDataGridView.CustomDataGrdView)sender);
        }

        public virtual void print_preview()
        {
        }

        protected virtual void DeleteRowGrid()
        {
            try
            {
                //int icurrRow = mdbg_currentGrid.CurrentCell.RowIndex;
                //DataSet ldsDelete = (DataSet)mdbg_currentGrid.DataSource;
                //ldsDelete.Tables[0].Rows[icurrRow].Delete();

                foreach (DataGridViewRow row in mdbg_currentGrid.SelectedRows)
                {
                    mdbg_currentGrid.Rows.Remove(row);
                }
            }
            catch (Exception ex)
            {
                Ctrl_LOG.Print("In FormBaseData Form", "At DeleteRowGrid method", "Error message: " + ex.Message);
            }
        }

        protected virtual void InsertRowGird()
        {

        }

        protected virtual void CopyRowsGird()
        {
            //Clipboard.SetDataObject(mdbg_currentGrid.SelectedRows[0], false);
            DataObject d = mdbg_currentGrid.GetClipboardContent();
            Clipboard.SetDataObject(d);
        }

        protected virtual void PastRowsGird()
        {
            //mdbg_currentGrid.Rows. [mdbg_currentGrid.CurrentCell.RowIndex] = (DataGridViewRow)Clipboard.GetDataObject();
            //mdbg_currentGrid.Rows.Add(Clipboard.GetDataObject());
        }

        public virtual void BaseDataForm_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
        {

        }

        public Control GetFocusedControl()
        {
            Control focusedControl = null;
            // To get hold of the focused control:
            IntPtr focusedHandle = GetFocus();
            if (focusedHandle != IntPtr.Zero)
                // Note that if the focused Control is not a .Net control, then this will return null.
                focusedControl = Control.FromHandle(focusedHandle);
            return focusedControl;
        }

        public void find()
        {
            /*if (mdbg_currentGrid == null) return;
            if (mMainForm != null)
            {
                // Checked the Find dialog is openning or not?
                bool lb_isOpened = false;
                foreach (Form lfrm in mMainForm.OwnedForms)
                    if (lfrm.Name == "FormFind")
                    {
                        lb_isOpened = true;
                        //lfrm.Show();
                        lfrm.Activate();
                        break;
                    }

                if (!lb_isOpened)
                {
                    FormFind lfrm_Find = new FormFind();
                    mMainForm.AddOwnedForm(lfrm_Find);
                    lfrm_Find.mfrmParentForm = this;
                    lfrm_Find.Show();
                }
            }*/
        }

        public bool CheckSecurity(string asTitle)
        {
            if (!mFormRight.mb_add || !mFormRight.mb_modify || !mFormRight.mb_delete)
            {
                MessageBox.Show("Access denied '" + asTitle + "'.Please contact Administrator HRM System!", ConstantKeys.Title, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            return true;
        }

        public bool FindNext(string as_findwhat, bool ab_matchCase, bool ab_FindUp)
        {	// Check the next CurrentRow 
            if (mdbg_currentGrid == null) return false;
            int li_currRow, li_currCol;
            string ls_forCompare;
            if (!ab_matchCase) as_findwhat = as_findwhat.ToUpper();
            try
            {
                if (ab_FindUp)
                {	// find up
                    li_currRow = mdbg_currentGrid.CurrentCell.RowIndex - 1;
                    li_currCol = mdbg_currentGrid.CurrentCell.ColumnIndex;
                    for (int li_rowIndex = li_currRow; li_rowIndex >= 0; li_rowIndex--)
                    {
                        if (mdbg_currentGrid[li_currCol, li_rowIndex].Value == null) continue;

                        if (ab_matchCase)
                            ls_forCompare = mdbg_currentGrid[li_currCol, li_rowIndex].Value.ToString().Trim();
                        else
                            ls_forCompare = mdbg_currentGrid[li_currCol, li_rowIndex].Value.ToString().Trim().ToUpper();

                        for (int li_sub = 0; li_sub < ls_forCompare.Length - as_findwhat.Length + 1; li_sub++)
                        {
                            if (ls_forCompare.Substring(li_sub, as_findwhat.Length) == as_findwhat)
                            {
                                mdbg_currentGrid[li_currCol, li_rowIndex].Selected = true;
                                return true;
                            }
                        }
                    }
                }
                else
                {	// find down
                    li_currRow = mdbg_currentGrid.CurrentCell.RowIndex + 1;
                    li_currCol = mdbg_currentGrid.CurrentCell.ColumnIndex;
                    for (int li_rowIndex = li_currRow; li_rowIndex < mdbg_currentGrid.Rows.Count; li_rowIndex++)
                    {	//
                        if (mdbg_currentGrid[li_currCol, li_rowIndex] == null) continue;
                        //if (mdbg_currentGrid.Columns[li_currCol].ValueItems.Presentation == C1.Win.C1TrueDBGrid.PresentationEnum.CheckBox) continue;

                        if (ab_matchCase) // UperCase or Not
                            ls_forCompare = mdbg_currentGrid[li_currCol, li_rowIndex].Value.ToString().Trim();
                        else
                            ls_forCompare = mdbg_currentGrid[li_currCol, li_rowIndex].Value.ToString().Trim().ToUpper();

                        // search the text in column
                        for (int li_sub = 0; li_sub < ls_forCompare.Length - as_findwhat.Length + 1; li_sub++)
                        {
                            if (ls_forCompare.Substring(li_sub, as_findwhat.Length) == as_findwhat)
                            {
                                mdbg_currentGrid[li_currCol, li_rowIndex].Selected = true;
                                return true;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error message: '" + ex.Message + "'");
                return false;
            } 
            return false;
        }

        public bool FindNext(string as_findwhat, bool ab_matchCase, bool ab_FindUp, int ai_CurrCol, int ai_CurrRow)
        {	// Check the next CurrentRow 
            if (mdbg_currentGrid == null) return false;
            int li_currRow, li_currCol;
            string ls_forCompare;
            if (!ab_matchCase) as_findwhat = as_findwhat.ToUpper();
            try
            {
                if (ab_FindUp)
                {	// find up
                    if (ai_CurrRow <= 0) return true;
                    li_currRow = ai_CurrRow - 1;
                    li_currCol = ai_CurrCol;
                    for (int li_rowIndex = li_currRow; li_rowIndex >= 0; li_rowIndex--)
                    {
                        if (mdbg_currentGrid[li_currCol, li_rowIndex].Value == null) continue;

                        if (ab_matchCase)
                            ls_forCompare = mdbg_currentGrid[li_currCol, li_rowIndex].Value.ToString().Trim();
                        else
                            ls_forCompare = mdbg_currentGrid[li_currCol, li_rowIndex].Value.ToString().Trim().ToUpper();

                        for (int li_sub = 0; li_sub < ls_forCompare.Length - as_findwhat.Length + 1; li_sub++)
                        {
                            if (ls_forCompare.Substring(li_sub, as_findwhat.Length) == as_findwhat)
                            {
                                mdbg_currentGrid.ClearSelection();
                                mdbg_currentGrid.FirstDisplayedScrollingRowIndex = li_rowIndex;
                                if (mdbg_currentGrid.Columns[0].Name.Equals("Selection")) mdbg_currentGrid["Selection", li_rowIndex].Value = true;
                                mdbg_currentGrid[li_currCol, li_rowIndex].Selected = true;
                                return true;
                            }
                        }
                    }
                }
                else
                {	// find down
                    if (ai_CurrRow >= mdbg_currentGrid.Rows.Count - 1) return true;
                    li_currRow = ai_CurrRow + 1;
                    li_currCol = ai_CurrCol;
                    for (int li_rowIndex = li_currRow; li_rowIndex < mdbg_currentGrid.Rows.Count; li_rowIndex++)
                    {	//
                        if (mdbg_currentGrid[li_currCol, li_rowIndex] == null) continue;
                        //if (mdbg_currentGrid.Columns[li_currCol].ValueItems.Presentation == C1.Win.C1TrueDBGrid.PresentationEnum.CheckBox) continue;

                        if (ab_matchCase) // UperCase or Not
                            ls_forCompare = mdbg_currentGrid[li_currCol, li_rowIndex].Value.ToString().Trim();
                        else
                            ls_forCompare = mdbg_currentGrid[li_currCol, li_rowIndex].Value.ToString().Trim().ToUpper();

                        // search the text in column
                        for (int li_sub = 0; li_sub < ls_forCompare.Length - as_findwhat.Length + 1; li_sub++)
                        {
                            if (ls_forCompare.Substring(li_sub, as_findwhat.Length) == as_findwhat)
                            {
                                mdbg_currentGrid.ClearSelection();
                                mdbg_currentGrid.FirstDisplayedScrollingRowIndex = li_rowIndex;
                                if (mdbg_currentGrid.Columns[0].Name.Equals("Selection")) mdbg_currentGrid["Selection", li_rowIndex].Value = true;
                                mdbg_currentGrid[li_currCol, li_rowIndex].Selected = true;
                                return true;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error message: '" + ex.Message + "'");
                return false;
            }
            return false;
        }
    }
}
